<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
import resize from './mixins/resize'

export default {
  name:  'ChartView',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },

    options: {
      type: Object,
      default: () => {}
    },
  },

	watch: {
		options: {
      deep: true,
      handler(val) {
        this.initChart(val);
			},
		},
	},

  data() {
    return {
      chartId: null,
      chart: null,
    }
  },
  mounted() {
    this.initListener()
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      if (!this.chart) {
        this.chart = echarts.init(this.$el)
      }
      this.chart.setOption(this.options)
    },
  }
}
</script>
